package fi.iki.joker.mobilogger;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import fi.iki.joker.mobilogger.db.DBConstants;
import fi.iki.joker.mobilogger.db.DBUtil;
import fi.iki.joker.mobilogger.db.SettingBean;
import fi.iki.joker.mobilogger.util.AlertUtil;

/**
 * This activity class allows user to get, edit and set the
 * global settings of this application.
 * 
 * The settings are not really used yet so this is more like a GUI mockup.
 * The biggest work here is probably the way he different types or attributes are handled.
 * I.e. String and other "free form" attributes can just be entered, spell checked etc,
 * but for "enumerated" types you need a spinner. This is not implemented yet.
 * 
 * @author juha
 *
 */

public class MobiLoggerSettings extends Activity implements
	View.OnClickListener, OnItemSelectedListener {
	
	
	private static final String TAG = "MobiLoggerSettings";
	private DBUtil db = null;
	private SettingBean languageBean = null;
	private SettingBean authorBean = null;
	private SettingBean imlibBean = null;

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.settings);
		//Lookup view components !! 
		//TODO:remove unneeded ones
		LinearLayout ll = (LinearLayout)findViewById(R.id.settingslayout);
		TextView SettingsLanguageLabel = (TextView)findViewById(R.id.SettingsLanguageLabel);
		Spinner SettingsLanguageSpinner = (Spinner)findViewById(R.id.SettingsLanguageSpinner);
		TextView SettingsDefaultPoster = (TextView)findViewById(R.id.SettingsDefaultPosterLabel);
		EditText SettingsDefaultPosterEditor = (EditText)findViewById(R.id.SettingsDefaultPosterEditor);
		TextView SettingsImageStorageLabel = (TextView)findViewById(R.id.SettingsImageStorageLabel);
		Spinner SettingsImageStorageSpinner = (Spinner)findViewById(R.id.SettingsImageStorageSpinner);
		Button SettingsSaveButton = (Button)findViewById(R.id.SettingsSaveButton);
		db = new DBUtil();
		Intent intent = this.getIntent();
		if(intent.getAction().equals(Intent.ACTION_VIEW)) {
			// Load the configuration variables and provide gui glue to change them
			if(db != null) {
				languageBean = db.getSettingBeanByKey(this, DBConstants.SETTING_KEY_LANGUAGE);
				authorBean = db.getSettingBeanByKey(this, DBConstants.SETTING_KEY_AUTHOR);
				imlibBean = db.getSettingBeanByKey(this, DBConstants.SETTING_KEY_IMAGELIB);
				if(languageBean == null || authorBean == null || imlibBean == null) {
					Log.e(TAG, "Bean remained null when querying settings from DB: "+
							"(L: "+languageBean+", A: "+authorBean+", I:"+imlibBean+")");
					AlertUtil.showAlert(this, "Error", "Could not query the complete settings information from the DB!");
					this.finish();
				}
				//data for the Language spinner
				ArrayAdapter<CharSequence> langSpinnerAdapter = new ArrayAdapter<CharSequence>(this, 
						android.R.layout.simple_spinner_dropdown_item);
				langSpinnerAdapter.add(languageBean.getSettingvalue());
				SettingsLanguageSpinner.setAdapter(langSpinnerAdapter);
				SettingsLanguageSpinner.setOnItemSelectedListener(this);
				//Data for the default poster editor
				//TODO:here we should add other languages if there's more available.
				SettingsDefaultPosterEditor.setText(authorBean.getSettingvalue());
				SettingsDefaultPosterEditor.setEnabled(true);
				// Data for the image lib selection spinner
				ArrayAdapter<CharSequence> imLibSpinnerAdapter = new ArrayAdapter<CharSequence>(this, 
						android.R.layout.simple_spinner_dropdown_item);
				imLibSpinnerAdapter.add(imlibBean.getSettingvalue());
				SettingsImageStorageSpinner.setAdapter(imLibSpinnerAdapter);
				SettingsImageStorageSpinner.setOnItemSelectedListener(this);
				//The save button
				SettingsSaveButton.setOnClickListener(this);
			} else {
				Log.e(TAG,"Failed to open database when querying for settings.");
				AlertUtil.showAlert(this, "Error" , "Failed to get db connection to query the settings data. ");
				this.finish();
			}
			
		} else if(intent.getAction().equals(Intent.ACTION_INSERT)) {
			// Save the changed values
		}
		
	}
	
	/**
	 * handle the save button click
	 */
	
	public void onClick(View arg0) {
		
	}

	/**
	 * Handle the spinner item selections
	 */
	
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * Handler the spinner nothing selected events
	 */
	
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	
	
}
